package com.example.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.dao.UserByAdminDao;
import com.example.entity.PageResult;
import com.example.entity.QueryPageBean;
import com.example.pojo.GoodsCategory;
import com.example.pojo.User;
import com.example.service.UserByAdminService;
import com.example.utils.MD5Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import sun.misc.BASE64Decoder;

import java.util.Date;

@Service(interfaceClass = UserByAdminService.class)
public class UserByAdminServiceImpl implements UserByAdminService {
    @Autowired
    private UserByAdminDao userByAdminDao;
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userByAdminDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(User user) {

        user.setPassword(MD5Utils.md5(user.getPassword()));
        user.setGrade(ByScoreToGrade(Integer.parseInt(user.getScore().toString())));
        user.setGmtCreate(new Date());
        user.setGmtModify(new Date());
        userByAdminDao.add(user);

    }

    @Override
    public void deleteById(Integer id) {
        userByAdminDao.deleteById(id);
    }

    @Override
    public User findById(Integer id) {
        return userByAdminDao.findById(id);
    }

    @Override
    public void edit(User user) {
        if (user.getPassword().length()<20)
        {
            user.setPassword(MD5Utils.md5(user.getPassword()));
        }
        user.setGrade(ByScoreToGrade(Integer.parseInt(user.getScore().toString())));
        user.setGmtModify(new Date());
        userByAdminDao.edit(user);
    }

    public static String ByScoreToGrade(Integer score)
    {
            if(score<=500)
                return "普通会员";
            else if(score<=2000)
                return "青铜会员";
            else if (score<=10000)
                return "白银会员";
            else if(score<=50000)
                return "黄金会员";
            else if (score<=100000)
                return "黑金会员";
            else
                return "钻石会员";
    }

}
